from django.http import HttpResponse
from django.shortcuts import render

# Create your views here.

import json

import requests
import re
from fake_useragent import UserAgent


class Crowler_xg:
    def get_data(self):
        headers = {
            'UserAgent': UserAgent().random
        }
        resp = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia?from=timeline&isappinstalled=0', headers=headers,
                            timeout=4)
        resp = str(resp.content, 'utf-8')
        areas_data = re.findall(r'try { window.getAreaStat = (.*?)}catch\(e\)', resp)
        areas_data_dic = json.loads(areas_data[0])
        return areas_data_dic

    def get_p_Name(self, areas_data):
        provinceName = []
        for res in areas_data:
            provinceName.append(res['provinceName'])
        return provinceName

    def get_c_Name(self, areas_data):
        cityName = []
        for res in areas_data:
            cityName.append(res['cityName'])
        return cityName

    def get_p_currentConfirmedCount(self, areas_data):
        currentConfirmedCount = []
        for res in areas_data:
            currentConfirmedCount.append(res['currentConfirmedCount'])
        return currentConfirmedCount

    def get_p_confirmedCount(self, areas_data):
        confirmedCount = []
        for res in areas_data:
            confirmedCount.append(res['confirmedCount'])
        return confirmedCount

    def get_p_curedCount(self, areas_data):
        curedCount = []
        for res in areas_data:
            curedCount.append(res['curedCount'])
        return curedCount

    def get_p_deadCount(self, areas_data):
        deadCount = []
        for res in areas_data:
            deadCount.append(res['deadCount'])
        return deadCount

    def get_cities(self, areas_data):
        cities = []
        for res in areas_data:
            cities.append(res['cities'])
        return cities


def crowler(request):
    xg = Crowler_xg()
    areas_data = xg.get_data()
    p_name = xg.get_p_Name(areas_data)
    p_xcqz = xg.get_p_currentConfirmedCount(areas_data)
    p_ljqz = xg.get_p_confirmedCount(areas_data)
    p_zy = xg.get_p_curedCount(areas_data)
    p_sw = xg.get_p_deadCount(areas_data)
    all_data = zip(p_name, p_xcqz, p_ljqz, p_zy, p_sw)
    return render(request, 'areas_data.html', locals())


def show_more(request):
    index = int(request.GET.get('tr', None))
    # print(index)
    xg = Crowler_xg()
    areas_data = xg.get_data()
    p_cities = xg.get_cities(areas_data)
    # print(p_cities[index])
    if len(p_cities[index]) is 0:
        msg = {
            'msg': '该地区的所有情况都已经展示在首页啦!!!'
        }
        return render(request, 'areas_data.html', msg)
    else:
        c_name = xg.get_c_Name(p_cities[index])
        c_xcqz = xg.get_p_currentConfirmedCount(p_cities[index])
        c_ljqz = xg.get_p_confirmedCount(p_cities[index])
        c_zy = xg.get_p_curedCount(p_cities[index])
        c_sw = xg.get_p_deadCount(p_cities[index])
        all_data = zip(c_name, c_xcqz, c_ljqz, c_zy, c_sw)
        return render(request, 'areas_data.html', locals())
